package com.baidu.bos.dao.base;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.transform.ResultTransformer;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.baidu.bos.utils.PageBean;


public class BaseDaoImpl<T> extends HibernateDaoSupport implements IBaseDao<T> {
	private Class<T> domainClass;
	
	public BaseDaoImpl(){
		//获得父类类型
		ParameterizedType genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass();
		//获得父类泛型的数组
		Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
		domainClass = (Class<T>) actualTypeArguments[0];
	}
	/**
	 * 注入sessionFactory
	 * @param sessionFactory
	 */
	@Resource
	public void setSF(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	
	public void save(T entity) {
		this.getHibernateTemplate().save(entity);
	}

	public void delete(T entity) {
		this.getHibernateTemplate().delete(entity);
	}

	public void update(T entity) {
		this.getHibernateTemplate().update(entity);
	}

	public T findById(Serializable id) {
		return this.getHibernateTemplate().get(domainClass, id);
	}

	public List<T> findAll() {
		String hql = "from " + domainClass.getSimpleName();
		return this.getHibernateTemplate().find(hql);
	}

	public List<T> findByCriteria(DetachedCriteria criteria) {
		return this.getHibernateTemplate().findByCriteria(criteria);
	}

	public List<T> findByNamedQuery(String queryName, Object... args) {
		return this.getHibernateTemplate().findByNamedQuery(queryName, args);
	}

	public void executeNamedQuery(String queryName, Object... args) {
		Session session = this.getSession();
		Query query = session.getNamedQuery(queryName);
		if (args != null && args.length>0) {
			int i = 0;
			for (Object arg : args) {
				query.setParameter(i++, arg);
			}
		}
		query.executeUpdate();
	}
	/**
	 * 分页条件查询
	 */
	public void pageQuery(PageBean pageBean) {
		int currentPage = pageBean.getCurrentPage();//当前页数
		int pageSize = pageBean.getPageSize();//每一页的记录数
		DetachedCriteria detachedCriteria = pageBean.getDetachedCriteria();//离线查询对象
		//需要查询出总的记录数
		detachedCriteria.setProjection(Projections.rowCount());
		List<Long> list = this.getHibernateTemplate().findByCriteria(detachedCriteria);
		Long total = list.get(0);//总记录数
		//将条件置为空
		detachedCriteria.setProjection(null);
		//查询当前页展示的数据集合
		detachedCriteria.setResultTransformer(DetachedCriteria.ROOT_ENTITY);
		List rows = this.getHibernateTemplate().findByCriteria(detachedCriteria, (currentPage - 1)*pageSize, pageSize);
		
		pageBean.setTotal(total.intValue());
		pageBean.setRows(rows);
	}

}
